package cn.tannn.tgateway.web.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * 网关 后置处理demo  观测 filter
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @date 2024/5/30 上午11:41
 */
@Component
@Slf4j
@Order
public class GatewayObservationWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        log.debug("===>>> spring [post] web observation  filter ...");
        // 上面的日志会先输出，下面的要等完成所有工作后才会处理 【doFinally】
        return chain.filter(exchange).doFinally(
                s -> {
                    log.info("===>>> [Observation] post filter");
                    exchange.getAttributes().forEach((k,v) -> log.info("{}:{}", k, v));
                }
        );
    }
}
